package zhihui.mobile.ui.shop.category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.category.CategoryInfo;


/**
 * Created by 张公铭 on 2017/9/14 0014.
 */

public class ListAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<CategoryInfo> mList = new ArrayList<>();


    public ListAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmList(List<CategoryInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_name;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.text_item, null);
            tv_name = (TextView) convertView.findViewById(R.id.tv_item_text);

            ViewCache viewCache = new ViewCache();
            viewCache.tv_name = tv_name;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            tv_name = viewCache.tv_name;
        }

        tv_name.setText(mList.get(position).getName());

        return convertView;
    }

    class ViewCache
    {
        public TextView tv_name;
    }
}
